package com.umeng.soexample.mvp.persenter;

import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.CouponListEntity;
import com.umeng.soexample.mvp.contract.CouponListFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CouponListFragmentPersenter extends BasePresenter<CouponListFragmentContract.CouponListFragmentIView, CouponListFragmentContract.CouponListFragmentIModel> {
    @Inject
    public CouponListFragmentPersenter(CouponListFragmentContract.CouponListFragmentIView couponListFragmentIView, CouponListFragmentContract.CouponListFragmentIModel couponListFragmentIModel) {
        super(couponListFragmentIView, couponListFragmentIModel);
    }

    public void request(String user_id){
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("user_id",user_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        m.coupon_list(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CouponListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CouponListEntity couponListEntity) {
                        v.showDialog();
                        v.success(couponListEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }
}
